#!/bin/sh
# Lame plugin for CueSplitter
# Nov. 2008 by Nathan Fisher

# Where are we?
PLUGINDIR="$APPDIR/plugins/lame"

# Get translations
ENVIRONMENT_LANG=`echo $LANG | sed "s/\(..\).*/\1/"` #detect environment language 
case $ENVIRONMENT_LANG in
	de)	export LANGUAGE="german" ;;
	en)	export LANGUAGE="english" ;;
	es)	export LANGUAGE="spanish" ;;
	fr)	export LANGUAGE="french" ;;
	it)	export LANGUAGE="italian" ;;
	nb)	export LANGUAGE="norwegian" ;; #translation is norwegian bokmål
	nl)	export LANGUAGE="dutch" ;;
	nn)	export LANGUAGE="norwegian" ;; #Use norwegian bokmål, since there is no norwegian nynorsk
	ru)	export LANGUAGE="russian" ;;
	*)	export LANGUAGE="english" ;; #english for contries without translation
esac
. $PLUGINDIR/locale/english.po #always run to fill gaps in translation
[ -f "$PLUGINDIR/locale/${LANGUAGE}.po" ] && . $PLUGINDIR/locale/${LANGUAGE}.po 2> /dev/null

# set the file extension
export EXT="mp3"

# Setup working space for plugin
[ ! -d "$HOME/.config/cuesplitter/plugins/lame" ] && mkdir "$HOME/.config/cuesplitter/plugins/lame"

# Plugin preferences
[ -f "$HOME/.config/cuesplitter/plugins/lame/preferences" ] && . "$HOME/.config/cuesplitter/plugins/lame/preferences"

 plugin_options() {
 export OPTS_DLG="
 <window title=\"$LOC_LAME_OPTS\" icon_name=\"audio-x-generic\">
  <vbox>
   <frame $LOC_QUALITY>
    <radiobutton draw_indicator=\"false\">
     <label>$LOC_VBR</label>
     <variable>VBR</variable>
     <action>if true enable:VBR_PRESET</action>
     <action>if false disable:VBR_PRESET</action>
     <action>if true enable:VBR_TEXT</action>
     <action>if false disable:VBR_TEXT</action>
     <action>if true disable:BITRATE</action>
     <action>if true disable:BITRATE_TEXT</action>
     <action>if false enable:BITRATE</action>
     <action>if false enable:BITRATE_TEXT</action>
    </radiobutton>
    <radiobutton draw_indicator=\"false\">
     <label>$LOC_ABR</label>
     <variable>ABR</variable>
    </radiobutton>
    <radiobutton draw_indicator=\"false\">
     <label>$LOC_CBR</label>
     <variable>CBR</variable>
    </radiobutton>
    <hbox>
     <text>
      <label>$LOC_VBR_PST</label>
      <variable>VBR_TEXT</variable>
     </text>
     <combobox>
      <item>$LOC_MEDIUM</item>
      <item>$LOC_STANDARD</item>
      <item>$LOC_EXTREME</item>
      <item>$LOC_INSANE</item>
      <variable>VBR_PRESET</variable>
     </combobox>
    </hbox>
    <hbox>
     <text>
      <label>$LOC_BITRATE</label>
      <variable>BITRATE_TEXT</variable>
      <visible>disabled</visible>
     </text>
     <combobox>
      <item>$BITRATE</item>
      <item>16</item>
      <item>24</item>
      <item>32</item>
      <item>40</item>
      <item>56</item>
      <item>64</item>
      <item>112</item>
      <item>128</item>
      <item>160</item>
      <item>192</item>
      <item>256</item>
      <variable>BITRATE</variable>
      <visible>disabled</visible>
     </combobox>
    </hbox>
    <hbox>
     <text>
      <label>$LOC_CHANNELS - </label>
     </text>
     <radiobutton>
      <label>$LOC_STEREO</label>
      <variable>STEREO</variable>
     </radiobutton>
     <radiobutton>
      <label>$LOC_MONO</label>
      <variable>MONO</variable>
     </radiobutton>
    </hbox>
   </frame>
   <hbox>
    <button>
     <input file stock=\"gtk-close\"></input>
     <label>$LOC_CLOSE</label>
    </button>
   </hbox>
  </vbox>
 </window>"
 gtkdialog3 --program OPTS_DLG > "$HOME/.config/cuesplitter/plugins/lame/preferences"
}
 convert_file() {
 
 # preset mode
 [ "$VBR" = "true" ] && MODE="$VBR_PRESET"
 [ "$ABR" = "true" ] && MODE="$BITRATE"
 [ "$CBR" = "true" ] && MODE="cbr $BITRATE"
 [ "$STEREO" = "true" ] && CHANNELS="s"
 [ "$MONO" = "true" ] && CHANNELS="m"
 
 # Convert to mp3 and tag audio files
 lame --preset $MODE -m $CHANNELS --ignore-tag-errors --tt "$TITLE" --ta "$ARTIST" --tl "$ALBUM" --ty "$DATE" --tc "$COMMENT" --tn "$TRACKNUM" --tg "$GENRE" "$FNAME" "`basename "$FNAME" .wav`.mp3"
}
